package com.hanlin.backend.common.util;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * md5加密器
 *
 * @author:hl.yuan
 * @date：2020-12-24
 */
public class MD5Utils {

    private static final String SALT = "hlyuan";

    private static final String ALGORITH_NAME = "md5";

    private static final int HASH_ITERATIONS = 2;

    /**
     * 加密
     *
     * @param pswd
     * @return
     */
    public static String encrypt(String pswd) {
        String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(SALT), HASH_ITERATIONS).toHex();
        return newPassword;
    }

    /**
     * 根据用户名加密
     *
     * @param username 用户名
     * @param pswd     密码
     * @return
     */
    public static String encrypt(String username, String pswd) {
        String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(username + SALT),
                HASH_ITERATIONS).toHex();
        return newPassword;
    }


    public static void main(String[] args) {

        System.out.println(MD5Utils.encrypt("hl.yuan", "111"));
    }
}
